<?php

/**
 * Helper class to format text
 */
class vtnsFormat
{
    /**
     * Code imported from
     * http://www.symfony-project.org/plugins/sfFormatSizePlugin/1_0_0
     * 
     * Originally adapted from
     * http://aidanlister.com/repos/v/function.size_readable.php
     * 
     * @param $size in bytes
     * @param $retstring
     */
    public static function size($size, $retstring = null)
    {
        $sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');

        if ($retstring === null) {
            $retstring = '%01.2f %s';
        }

        $lastsizestring = end($sizes);

        foreach ($sizes as $sizestring) {
            if ($size < 1024) {
                break;
            }

            if ($sizestring != $lastsizestring) {
                $size /= 1024;
            }
        }

        if ($sizestring == $sizes[0]) {
            $retstring = '%01d %s';
        }

        return sprintf($retstring, $size, $sizestring);
    }
}

?>
